﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace RISMOTAXDP.EmailService.MailProcess
{
    /// <summary>
    /// 前置邮件处理对象
    /// </summary>
    public class MailForwardProcess
    {
        /// <summary>
        /// 前置邮件检查
        /// </summary>
        private const string SQL_SELECT_MAIL = @"select ID, Title,Address,Content,Type,DateSent from _TBL_P_QueuingMails where Type<>'C1' ";
        private const string SQL_INSERT_MAIL = "INSERT INTO TBL_MailForward(Mail_ID,OperationCode,MailSubject,MailRcpt,MailContent,MailCreateDate) VALUES (@MailID,@OperationCode,@MailSubject,@MailRcpt,@MailContent,@MailCreateDate)";
        private const string SQL_DELETE = "delete _TBL_P_QueuingMails where id=@id";


        public void Process(SqlConnection conn)
        {
            //检查邮件
            DataSet ds = null;

            try
            {
                ds = SQLHelper.ExecuteDataset(conn, CommandType.Text, SQL_SELECT_MAIL);
            }
            catch (Exception ex)
            {
                GlobalObject.WriteOperationLog("E86000001", ex);
            }

            if (ds != null)
            {
                if (ds.Tables[0].Rows.Count != 0)
                {
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        DataRow dataRow = ds.Tables[0].Rows[i];
                        //从临时表中取出后，存入前置邮件列表中
                        //OperationCode,MailSubject,MailRcpt,MailContent,MailCreateDate
                        int MailID = (Int32)dataRow["id"];
                        Guid guidMailID = Guid.NewGuid();
                        string OperationCode = dataRow["Type"].ToString().ToUpper();
                        string MailSubject = dataRow["Title"].ToString();
                        string MailRcpt = dataRow["Address"].ToString();
                        string MailContent = dataRow["Content"].ToString();
                        DateTime MailCreateDate = (DateTime)dataRow["DateSent"];

                        SqlParameter[] sParms = { new SqlParameter("@MailID", guidMailID),
                                                    new SqlParameter("@OperationCode", OperationCode),
                                                    new SqlParameter("@MailSubject", MailSubject),
                                                    new SqlParameter("@MailRcpt", MailRcpt),
                                                    new SqlParameter("@MailContent", MailContent),
                                                    new SqlParameter("@MailCreateDate", MailCreateDate)
                        
                                                };

                        SQLHelper.ExecuteNonQuery(conn, CommandType.Text, SQL_INSERT_MAIL, sParms);

                        SqlParameter[] sParms_delete = { new SqlParameter("@id", MailID) };
                        SQLHelper.ExecuteNonQuery(conn, CommandType.Text, SQL_DELETE, sParms_delete);





                    }
                    GlobalObject.WriteSystemLog("T81000003", null, ds.Tables[0].Rows.Count);

                }
            }

        }

    }
}
